iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 6
0
Modern Web

使用Gatsby督促我的姊姊成為部落客!!!系列 第 6

Day 06 - 解決 404 not fount on root URL

  • 分享至 

  • xImage
  •  

在 wordpree 後台新增一個 page 為 home

在 gatsby-node.js 新增 redirect

 createRedirect({
    fromPath: "/",
    toPath: "/home",
    redirectInBrowser: true,
    isPermanent: true,
  })
const path = require(`path`)
const slash = require(`slash`)
 
// Implement the Gatsby API “createPages”. This is
// called after the Gatsby bootstrap is finished so you have
// access to any information necessary to programmatically
// create pages.
// Will create pages for WordPress pages (route : /{slug})
// Will create pages for WordPress posts (route : /post/{slug})
exports.createPages = async ({ graphql, actions }) => {
	const { createPage, createRedirect } = actions
	// createRedirect({ fromPath: '/', toPath: '/home', redirectInBrowser: true, isPermanent: true })
 
	// The “graphql” function allows us to run arbitrary
	// queries against the local Gatsby GraphQL schema. Think of
	// it like the site has a built-in database constructed
	// from the fetched data that you can run queries against.
	const result = await graphql(`
		{
			wordpressPage(title: { eq: "Home" }){
				id
				link
				status
				template
				slug
				title
				content
			}
			allWordpressPage(filter: { title: { ne: "Home" } }){
				edges{
					node{
						id
						title
						content
						slug
						status
						template
					}
				}
			}
			allWordpressPost {
				edges {
					node {
						id
						title
						content
						slug
						status
						template
						format
					}
				}
			}
		}
	`)
 
	// Check for any errors
	if (result.errors) {
		throw new Error(result.errors)
	}
 
	// Access query results via object destructuring
	const { wordpressPage, allWordpressPage, allWordpressPost } = result.data
 
	// Create Page pages.
	const pageTemplate = path.resolve(`./src/templates/page.js`)
	// We want to create a detailed page for each page node.
	// The path field contains the relative original WordPress link
	// and we use it for the slug to preserve url structure.
	// The Page ID is prefixed with 'PAGE_'
	createPage({
    path: `/`,
    component: slash(pageTemplate),
    context: wordpressPage
  })
	allWordpressPage.edges.forEach(edge => {
		// Gatsby uses Redux to manage its internal state.
		// Plugins and sites can use functions like "createPage"
		// to interact with Gatsby.
		createPage({
			// Each page is required to have a `path` as well
			// as a template component. The `context` is
			// optional but is often necessary so the template
			// can query data specific to each page.
			path: `/${edge.node.slug}/`,
			component: slash(pageTemplate),
			context: edge.node,
		})
	})
 
	const postTemplate = path.resolve(`./src/templates/post.js`)
	// We want to create a detailed page for each post node.
	// The path field stems from the original WordPress link
	// and we use it for the slug to preserve url structure.
	// The Post ID is prefixed with 'POST_'
	allWordpressPost.edges.forEach(edge => {
		createPage({
			path: `/${edge.node.slug}/`,
			component: slash(postTemplate),
			context: edge.node,
		})
	})
}

上一篇
Day 05 - 動態產生 Gatsby 頁面,來自 Wordpress 的 post page 與現有的 page
下一篇
Day 07 - 研究讀取 markdown 的設定
系列文
使用Gatsby督促我的姊姊成為部落客!!!13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言